import type { App } from 'vue';
import type { I18nOptions } from 'vue-i18n';
import { createI18n } from 'vue-i18n';

import enUS from './lang/en-US';
import jaJP from './lang/ja-JP';
import zhCN from './lang/zh-CN';
import zhTW from './lang/zh-TW';
import { useLocale } from './useLocale';

// 获取i18n消息内容 key 不能乱写，查看截图 screenshots/i18n.png
export const messages = {
  ...enUS,
  ...jaJP,
  ...zhCN,
  ...zhTW,
};

export let i18n: ReturnType<typeof createI18n>;

function createI18nOptions(): I18nOptions {
  // const locale = uni.getLocale()
  const { getLocale } = useLocale();

  return {
    // legacy: false,
    locale: getLocale(),
    messages,
    sync: true, // If you don’t want to inherit locale from global scope, you need to set sync of i18n component option to false.
    silentTranslationWarn: true, // true - warning off 去除国际化警告
    missingWarn: false,
    silentFallbackWarn: true,
  };
}

/** setup i18n instance with glob */
export function setupI18n(app: App) {
  const options = createI18nOptions();
  i18n = createI18n(options);

  app.use(i18n);
}
